package com.common.core.utils;

import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Tool {

	/**
	 * Encoding a string by MD5.
	 *
	 * @param key
	 *            The string need to be encoded.
	 * @return The resultant string.
	 */
	public static String getStringMD5(String key) {
		String value = null;
		MessageDigest currentAlgorithm;
		try {
			currentAlgorithm = MessageDigest.getInstance("MD5");
			currentAlgorithm.reset();
			currentAlgorithm.update(key.getBytes());
			byte[] hash = currentAlgorithm.digest();
			String d = "";
			int usbyte = 0; // unsigned byte
			for (int i = 0; i < hash.length; i += 2) { // format with 2-byte
				// words with spaces. test
				usbyte = hash[i] & 0xFF; // byte-wise AND converts signed byte
				// to unsigned.
				if (usbyte < 16)
					d += "0" + Integer.toHexString(usbyte); // pad on left if
					// single hex digit.
				else
					d += Integer.toHexString(usbyte);
				usbyte = hash[i + 1] & 0xFF; // byte-wise AND converts signed
				// byte to unsigned.
				if (usbyte < 16)
					d += "0" + Integer.toHexString(usbyte);// + " "; // pad on
					// left if single hex
					// digit.
				else
					d += Integer.toHexString(usbyte);// + " ";
			}
			// return d.toUpperCase();
			value = d.trim().toLowerCase();

		} catch (NoSuchAlgorithmException e) {
			System.out.println("MD5 algorithm not available.");
		}
		return value;
	}
	public static String getMd5ByFile(File file) {
		FileInputStream fis = null;
		MessageDigest md5 = null;
		try {
			md5 = MessageDigest.getInstance("MD5Tool");
			fis = new FileInputStream(file);
			byte[] buffer = new byte[8192];
			int length;
			while ((length = fis.read(buffer)) != -1) {
				md5.update(buffer, 0, length);
			}
			BigInteger bi = new BigInteger(1, md5.digest());
			return bi.toString(16);
		} catch (FileNotFoundException e) {
			Log.e("md5 file ", file.getAbsolutePath() + " failed:" + e.getMessage());
			return null;
		} catch (IOException e) {
			Log.e("md5 file ", file.getAbsolutePath() + " failed:" + e.getMessage());
			return null;
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		} finally {
			try {
				if (fis != null) {
					fis.close();
					fis = null;
				}

				if (md5 != null) {
					md5.reset();
					md5 = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static String toMd5(String md5Str) {
		String result = "";
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.reset();
			md5.update(md5Str.getBytes("utf-8"));
			result = toHexString(md5.digest());
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return result;
	}

	private static String toHexString(byte[] bytes) {
		StringBuilder hexString = new StringBuilder();

		for (int b : bytes) {
			if (b < 0)
				b += 256;
			if (b < 16)
				hexString.append("0");
			hexString.append(Integer.toHexString(b));
		}
		return hexString.toString();
	}
}
